package xzy.com.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import xzy.com.entity.domain.Article;
import xzy.com.entity.domain.Notice;
import xzy.com.entity.vo.ResponseBean;
import xzy.com.service.NoticeService;
/**
 * 
* @Title: NoticeController
* @Description: 新闻公告控制层
* @author: liyang
* @date 2020年12月6日 下午4:50:47
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
	@Autowired
	private NoticeService noticeService;
	

	// 分页处理
	@GetMapping("/page")
	public ResponseBean listPages(@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Notice> page = new Page<>(pages, size);
		QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByDesc("create_time");
		noticeService.page(page, queryWrapper);
		Page<Notice> pages1 = noticeService.page(page, queryWrapper);
		
		Map<String,Object> map = new HashMap<>();
		map.put("list", pages1.getRecords());
		map.put("recordTotal", pages1.getTotal());
		map.put("pageTotal", pages1.getPages());
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}

	// 搜索查询分页处理
	@GetMapping("/search")
	public ResponseBean  searchListPages(@RequestParam("title") String title,@RequestParam("pages") Integer pages, @RequestParam("size") Integer size) {
		Page<Notice> page = new Page<>(pages, size);
		QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
		queryWrapper.orderByAsc("id");
		if(null!=title) {
			queryWrapper.like("title", title);
		}
		Page<Notice> pages1 = noticeService.page(page, queryWrapper);
        //IPage<Menu> menuPage = menuService.findParentMenu(pages1, queryWrapper);
		//查询父级菜单
				
		Map<String,Object> map = new HashMap<>();
		map.put("list", pages1.getRecords());
		map.put("recordTotal", pages1.getTotal());
		map.put("pageTotal", pages1.getPages());
		return new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
	}
	
	// 根据id查询
		@GetMapping("/findById")
		public ResponseBean findById(@RequestParam("id") Long id) {
			if (null != id) {
				Notice notice = noticeService.getById(id);
				notice.setHittimes(notice.getHittimes()+1);
				noticeService.updateById(notice);
				if (notice != null) {
					return new ResponseBean(HttpStatus.OK.value(), "查询成功!", notice);
				} else {
					return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "查询失败", null);
				}
			}
			return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "id不能为空", null);
		}
	
	
}
